#!/usr/bin/env python
# encoding: utf-8
# @author: Lin Han
# @contact: voldemort22@126.com
# @file: deprecated_app.py
# @time: 2021/6/11 10:47
# @desc:
import json
import os

import zipfile
from fastapi import APIRouter, Request, BackgroundTasks

from utils.oss.oss_tools import download_file_from_oss

do_drawing_router = APIRouter(
    prefix="/api/do_drawing",
    tags=["do_drawing"]
)


@do_drawing_router.get("/v1/assemble_parts")
def transfer(zipfile_address: str):
    """

    - :param number: 编号
    - :return:
    """
    print(os.getcwd())
    parent_dir = ("data")
    local_file_name = os.path.join(parent_dir, zipfile_address)
    download_file_from_oss(
        oss_file_name=zipfile_address,
        local_file_name=local_file_name
    )

    f = zipfile.ZipFile(local_file_name)

    for file in f.namelist():
        print(file)
        f.extract(file, r'.')

    (path, filename) = os.path.split(local_file_name)

    filepath = os.path.join(path, filename.replace(".zip", ""), filename.replace(".zip", ".CATProduct"))
    # jsonfile = extract_json_of_directory(filepath, os.getcwd())
    return filepath
